package clark.model

import org.apache.log4j.Logger
/**
 * Created by IntelliJ IDEA.
 * Date: 11-3-1
 * Time: 上午10:46
 * @author Cui Gang
 * email:<a href="javascript:mailto">clarkhillm@gmail.com</a>
 */

object Vanguard {

    val connection = Tools.connection

    val ddl = """create table note(
                    id bigint generated always as identity,
                    date TIMESTAMP,
                    text clob,
                    title varchar (50),
                    catalog varchar (50),
                    primary key (id)
                 )
  """
    val logger = Logger.getLogger(this.getClass.getSimpleName)

    def buildTable {
        if (!isExist) {
            connection.createStatement.execute(ddl)
            logger.info("建表成功")
        }
    }

    def isExist: Boolean = {
        val rs = connection.createStatement.executeQuery("select * from sys.systables ")
        var tableNames = List[String]()
        while (rs.next) {
            tableNames = tableNames :+ rs.getObject("tablename").toString
        }
        val value = for (name <- tableNames if name.equalsIgnoreCase("note")) yield name

        if (value.length > 0) {
            logger.info("发现表note已经存在")
            true
        } else {
            logger.info("表note不存在，开始建表")
            false
        }
    }
}